<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>登录界面</title>
  <link rel="stylesheet" href="./css/style.css">
  <link rel="stylesheet" href="./css/body.css">
</head>
<style>
  p {
    margin-right: 252px;
    color: red;
  }
</style>

<body>
  <div class="container">
    <section id="content">
      <form>
        <h1>账号登录</h1>
        </h1>
        <div>
          <i class="close">X</i>
          <input type="text" placeholder="手机号" id="username">
        </div>
        <div>
          <i class="close">X</i>
          <input type="password" placeholder="密码" id="password">
        </div>
        <div>
          <input type="submit" value="登录" class="btn btn-primary" id="js-btn-login">
          <a href="">忘记密码?</a>
          <a href="#">注册账号</a>
        </div>
      </form>
    </section>
  </div>
</body>
<script>
  const phone = document.querySelector('#username')
  const psd = document.querySelector('#password')



  const div1 = phone.parentNode
  const div2 = psd.parentNode

  const p1 = document.createElement('p')
  const p2 = document.createElement('p')

  div1.appendChild(p1)
  div2.appendChild(p2)

  phone.addEventListener('change', verifyPhone)
  function verifyPhone() {

    // 准备正则
    const reg = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/

    if (!reg.test(phone.value) && phone.value.trim() === '') {
      return
    } else if (!reg.test(phone.value)) {
      p1.innerHTML = `请输入手机号`
      return false
    }
    p1.innerHTML = ``
    return true

  }
  psd.addEventListener('change', verifypsd)
  function verifypsd() {

    // 准备正则
    const reg = /^[a-zA-Z0-9-_]{6,20}$/

    if (!reg.test(psd.value) && psd.value.trim() === '') {
      return
    } else if (!reg.test(psd.value)) {
      p2.innerHTML = `请输入密码`
      return false
    }
    p2.innerHTML = ``
    return true

  }

  const one = phone.previousElementSibling
  const two = psd.previousElementSibling

  phone.addEventListener('input', function () {
    if (phone.value) {
      one.style.display = 'block'
    } else {
      one.style.display = 'none'
    }
  })

  psd.addEventListener('input', function () {
    if (psd.value) {
      two.style.display = 'block'
    } else {
      two.style.display = 'none'
    }
  })

  const close = document.querySelectorAll('.close')
  for (let i = 0; i < close.length; i++) {
    close[i].addEventListener('click', function () {
      this.style.display = 'none'
      this.nextElementSibling.value = ''
    })
  }

  const form = document.querySelector('form')
  form.addEventListener('submit', function (e) {
    e.preventDefault()
    const res1 = verifyPhone()
    const res2 = verifypsd()
    if (res1 && res2) {
      alert(`
            登录账号:${phone.value},登录密码:${psd.value}
            `)
    } else {
      alert('登录失败')
      phone.value = ''
      psd.value = ''
      p1.innerHTML = `请输入手机号`
      p2.innerHTML = `请输入密码`
    }
  })
</script>



</html>